package javango.polls;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static javango.contrib.freemarker.Helper.renderToResponse;
import org.apache.log4j.Logger;

import static javango.contrib.compat.JavangoStatic.Javango;
import javango.polls.forms.VoteForm;

import javango.contrib.compat.ModelDao;
import javango.contrib.hibernate.DaoException;
import javango.db.Manager;
import javango.db.ManagerException;
import javango.polls.model.Choice;
import javango.polls.model.Poll;
import javango.http.HttpException;
import javango.http.HttpRequest;
import javango.http.HttpResponse;
import javango.http.HttpResponseRedirect;
import javango.http.SimpleHttpResponse;

public class Views {

	private final static Logger log = Logger.getLogger(Views.class);

	/**
	 * 
	 * @param request
	 * @param response
	 * @return
	 */
	public HttpResponse index(HttpRequest request) throws HttpException {
		try {
			Manager<Poll> polls = Javango().Managers().forClass(Poll.class);
			List<Poll> latest_poll_list = polls.all().list();
			
			Map<String, Object> context = new HashMap<String, Object>();
			context.put("latest_poll_list", latest_poll_list);
			
			return renderToResponse("javango/polls/templates/index.ftl", context);
			
		} catch (ManagerException e) {
			throw new HttpException(e);
		}
	}
	
	public HttpResponse detail(HttpRequest request, Long poll_id) throws HttpException {
		try {
			Manager<Poll> polls = Javango().Managers().forClass(Poll.class);
			
			Poll p = new ModelDao<Poll>(Poll.class).get(poll_id);
			if (p == null) {
				return new SimpleHttpResponse("Unable to find poll with id = " + poll_id);
			}
			
			VoteForm form = new VoteForm(p);
			
			Map<String, Object> context = new HashMap<String, Object>();
			context.put("poll", p);
			context.put("form", form);
										
			return renderToResponse("javango/polls/templates/detail.ftl", context);
		} catch (DaoException e) {
			throw new HttpException(e);
		}
	}

	public HttpResponse results(HttpRequest request, Long poll_id) throws HttpException {
		Poll p = Javango().getObjectOr404(Poll.class, poll_id);
		
		Map<String, Object> context = new HashMap<String, Object>();
		context.put("poll", p);
		
		return renderToResponse("javango/polls/templates/results.ftl", context);

	}
	
	public HttpResponse vote(HttpRequest request, Long poll_id) throws HttpException {
		Poll p = Javango().getObjectOr404(Poll.class, poll_id);
		
		VoteForm form = Javango().newForm(VoteForm.class);
		form.bind(request.getParameterMap());			
		
		if (!form.isValid()) {
			Map<String, Object> context = new HashMap<String, Object>();
			context.put("poll", p);
			context.put("error_message", "You didn't select a valid choice");
			context.put("form", form);
			return renderToResponse("javango/polls/templates/detail.ftl", context);
		}
		
		Choice selected_choice = (Choice)form.getCleanedData().get("choice");

		int votes = selected_choice.getVotes() == null ? 0 : selected_choice.getVotes().intValue();
		selected_choice.setVotes(votes + 1);
		// due to the way hibernate works this will automatically save when the middleware commits
		return new HttpResponseRedirect("/polls/" + poll_id + "/results/");
	}
}
